package data;

import java.util.LinkedList;
import javax.swing.ImageIcon;

public class LayMines {
    ImageIcon mineIcon;

    public LayMines() {
        mineIcon = new ImageIcon("material/13.gif");
    }

    public void initBlock(Block[][] block) {
        for (int i = 0; i < block.length; i++) {
            for (int j = 0; j < block[i].length; j++)
                block[i][j].setIsMine(false);
        }
    }

    public void layMinesForBlock(Block[][] block, int mineCount) { //在雷区布置 mineCount 个雷
        initBlock(block);       //先都设置是无雷
        int row = block.length;
        int column = block[0].length;
        LinkedList<Block> list = new LinkedList<Block>();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++)
                list.add(block[i][j]);
        }
        while (mineCount > 0) {                  //开始布雷
            int size = list.size();             // list 返回节点的个数
            int randomIndex = (int) (Math.random() * size);
            Block b = list.get(randomIndex);
            b.setIsMine(true);
            b.setName("雷");
            b.setMineIcon(mineIcon);
            list.remove(randomIndex);        //list 删除索引值为 randomIndex 的节点
            mineCount--;
        }
        for (int i = 0; i < row; i++) {             //检查布雷情况，标记每个方块周围的雷的数目
            for (int j = 0; j < column; j++) {
                if (block[i][j].isMine()) {
                    block[i][j].setIsOpen(false);
                    block[i][j].setIsMark(false);
                    block[i][j].setMineIcon(new ImageIcon("material/" + 13 + ".gif"));
                } else {
                    int mineNumber = 0;
                    for (int k = Math.max(i - 1, 0); k <= Math.min(i + 1, row - 1); k++) {
                        for (int t = Math.max(j - 1, 0); t <= Math.min(j + 1, column - 1); t++) {
                            if (block[k][t].isMine())
                                mineNumber++;
                        }
                    }
                    block[i][j].setIsOpen(false);
                    block[i][j].setIsMark(false);
                    block[i][j].setName(Integer.toString(mineNumber));
                    block[i][j].setAroundMineNumber(mineNumber); //设置该方块周围的雷数目
                    block[i][j].setMineIcon(new ImageIcon(new String("material/" + mineNumber + ".gif")));
                }
            }
        }
    }
}
